// 模块：销售品设计/销售品关联关系

// // 定义模板
// const temp_pagination = `
//   <div class="pagination">
//     <!-- 是否禁用 判断当前页码是否是 等于 1 -->
//     <a
//       href="javascript:;"
//       :class="{disabled: myCurrentPage <= 1}"
//       @click="changePage(myCurrentPage - 1)"
//     >上一页</a>
//     <!-- 是否显示 判断起始页码是否 大于 1 -->
//     <span v-if="pager.start>1">...</span>
//     <a
//       v-for="i in pager.btnArr"
//       :key="i"
//       href="javascript:;"
//       :class="{active: i === myCurrentPage}"
//       @click="changePage(i)"
//     >{{ i }}</a>
//     <!-- 是否显示 判断 结束页码 是否 小于总页数 -->
//     <span v-if="pager.end < pager.pageCount">...</span>
//     <!-- 是否禁用 判断当前页码 是否 大于等于 总页数 -->
//     <a
//       href="javascript:;"
//       :class="{disabled: myCurrentPage >= pager.pageCount}"
//       @click="changePage(myCurrentPage + 1)"
//     >下一页</a>
//   </div>`

// 定义app
const OfferRelation = {
    delimiters: ['${', '}'],
    data() {
        return {
            sites: [
                {id: 1, title: 'Google'},
                {id: 2, title: 'Runoob'},
                {id: 3, title: 'Taobao'}
            ],
            resList: {
                "rel-az": [],  // 点对点关系AZ
                "rel-za": [],  // 点对点关系ZA
                "grp-inner-mutex": [],  // 组内互斥
                "grp-rel-az": [],  // 组间关系AZ
                "grp-rel-za": []  // 组间关系ZA
            }
        }
    },
    methods: {
        // 刷新列表
        list_refresh: function (cad_id, offer_id) {
            this.list_offer_rel(cad_id, offer_id, 'az');
            this.list_offer_rel(cad_id, offer_id, 'za');
            this.list_grp_rel(cad_id, offer_id, 'az');
            this.list_grp_rel(cad_id, offer_id, 'za');
            this.list_grp_inner(cad_id, offer_id);
        },
        // 列举点对点关系
        list_offer_rel: function (cad_id, offer_id, direction) {
            if (!['az', 'za'].includes(direction)) {
                return false;
            }
            let elId = 'rel_az';
            if (direction == 'za') {
                elId = 'rel_za';
            }
            axios
                .get('api/v1/list-cad-offer-rel/' + cad_id + '/' + offer_id + '/' + direction)
                .then(response => (this.resList["rel-" + direction] = response.data.result), clear_all(elId))
                .catch(function (error) {
                    console.log(error);
                })
        },
        // 列举组间关系
        list_grp_rel: function (cad_id, offer_id, direction) {
            if (!['az', 'za'].includes(direction)) {
                return false;
            }
            let elId = 'grp_a_id';
            if (direction == 'za') {
                elId = 'grp_z_id';
            }
            axios
                .get('api/v1/list-cad-offer-grp-rel/' + cad_id + '/' + offer_id + '/' + direction)
                .then(response => (this.resList["grp-rel-" + direction] = response.data.result), clear_all(elId))
                .catch(function (error) {
                    console.log(error);
                })
        },
        // 列举组内关系
        list_grp_inner: function (cad_id, offer_id) {
            axios
                .get('api/v1/list-cad-offer-grp-inner/' + cad_id + '/' + offer_id)
                .then(response => (this.resList["grp-inner-mutex"] = response.data.result), clear_all('grp_id'))
                .catch(function (error) {
                    console.log(error);
                })
        },
        // 退出组
        out_offer_grp(elementId, cad_id, offer_id) {
            if (!['#grp_id', '#grp_z_id', '#grp_a_id'].includes(elementId)) {
                return false;
            }
            let grp_id = document.querySelectorAll(elementId + ':checked')
            if (grp_id.length == 0) {
                alert('请先选择')
                return false;
            }
            axios({
                method: 'post', url: 'api/v1/out-offer-grp',
                data: {
                    cad_id: cad_id,
                    offer_id: offer_id,
                    grp_id: grp_id
                }
            })
                .then(response => {
                    console.log(response.data);
                    // alert(response.data.code);
                    if (elementId == '#grp_id') {
                        this.list_grp_inner(cad_id, offer_id); // 重新列举组内关系
                    } else if (elementId == '#grp_a_id') {
                        this.list_grp_rel(cad_id, offer_id, 'az'); // 重新列举组间AZ关系
                    } else if (elementId == '#grp_z_id') {
                        this.list_grp_rel(cad_id, offer_id, 'za'); // 重新列举组间ZA关系
                    }
                })
                .catch(function (error) {
                    console.log(error);
                    alert('【操作失败】\n' + error)
                })
        }
    },
    // components: {
    //     'site-info': {
    //         template: temp_pagination
    //     }
    // }
}


// 创建Vue应用
Vue.createApp(OfferRelation).mount('#collapse-offer-relation')